package renda.action.user;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import renda.common.SessionUtil;
import renda.dao.serivce.TopicChildService;
import renda.dao.serivce.TopicsService;
import renda.pojo.Topicchild;
import renda.pojo.Topics;

import com.opensymphony.xwork2.ActionSupport;

public class TopicchildAction extends ActionSupport {

	private int page = 1;
	private int size = 10;

	private Topicchild topicchild;

	private Integer topicId;

	private TopicsService topicsService;

	private TopicChildService topicChildService;

	public String list() {
		// TODO
		page = (page - 1) * size;
		Topics topic = topicsService.getById(topicId);
		ServletActionContext.getRequest().setAttribute("topic", topic);
		List<Topicchild> children = topicChildService.listByTopicId(topicId, page, size);
		// if (children != null && children.size() > 0) {
		// for (Topicchild topicchild : children) {
		// if (topicchild.getContent().length() > 20) {
		// topicchild.setContent(topicchild.getContent().substring(0, 20) + "...");
		// }
		// }
		// }
		ServletActionContext.getRequest().getSession().setAttribute("children", children);
		return "success";
	}

	public String save() {
		// TODO
		topicchild.setContent(topicchild.getContent().trim());
		topicchild.setReturntime(new Date());
		// topicchild.setWriter(SessionUtil.getCurrentUsers().getUname());
		topicChildService.saveTopicchild(topicchild);

		Integer topicId = topicchild.getTopicid();
		Integer userType = SessionUtil.getCurrentUserType();
		topicsService.updateTopics(topicId, userType);
		return "success";
	}

	// public void validateSave() {
	// if (topicchild != null) {
	// boolean isError = false;
	// if (StringUtils.isBlank(topicchild.getContent())) {
	// isError = true;
	// ServletActionContext.getRequest().setAttribute("topiccontenterror", "意见建议不能为空");
	// this.addFieldError("topiccontenterror", "意见建议不能为空");
	// } else if (topicchild.getContent().length() > 4000) {
	// isError = true;
	// ServletActionContext.getRequest().setAttribute("topiccontenterror", "意见建议长度最大为4000个字符");
	// this.addFieldError("topiccontenterror", "意见建议长度最大为4000个字符");
	// }
	// if (isError) {
	// ServletActionContext.getRequest().setAttribute("topicchild", topicchild);
	// }
	// }
	// }

	public Topicchild getTopicchild() {
		return topicchild;
	}

	public void setTopicchild(Topicchild topicchild) {
		this.topicchild = topicchild;
	}

	public TopicChildService getTopicChildService() {
		return topicChildService;
	}

	public void setTopicChildService(TopicChildService topicChildService) {
		this.topicChildService = topicChildService;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public Integer getTopicId() {
		return topicId;
	}

	public void setTopicId(Integer topicId) {
		this.topicId = topicId;
	}

	public TopicsService getTopicsService() {
		return topicsService;
	}

	public void setTopicsService(TopicsService topicsService) {
		this.topicsService = topicsService;
	}

}
